/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.commons;

import java.io.Serializable;
import javax.swing.JPanel;
import org.balisunrise.common.EntityGrid;
import org.balisunrise.common.EntityActions;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.common.abstracts.AbstractVisualEntity;
import org.balisunrise.swing.components.panel.SelectionPanel;

/**
 * Utilizado para selecionar uma entidade de uma lista de entidades.
 * Possui um EntityGrid para listar as entidades e um EntityAction para
 * adicionar ações de Confirmar e Cancelar.
 * Para o EntityGrid deve-se adicionar o EntityLoader antes de atribuir a
 * objetos desta classe.
 *
 * @author Glauco
 */
public class EntitySelection extends AbstractVisualEntity
        implements org.balisunrise.common.EntitySelection<
            EntityGrid, EntityActions<?,? extends JPanel>, JPanel>{

    public static final String PROP_VISUAL_REPRESENTATION = "visualRepresentation";
    public static final String PROP_ENTITY_GRID = "entityGrid";
    public static final String PROP_ENTITY_ACTIONS = "entityActions";

    private SelectionPanel visualRepresentation;
    private EntityGrid entityGrid;
    private EntityActions<?,? extends JPanel> entityActions;

    /**
     * Atribui o EntityGrid.
     *
     * @param entityGrid
     */
    @Override
    public void setEntityGrid(EntityGrid entityGrid) {
        EntityGrid old = this.entityGrid;
        this.entityGrid = entityGrid;
        changeSupport.firePropertyChange(PROP_ENTITY_GRID, old, entityGrid);
    }

    /**
     * Retorna o EntityGrid.
     */
    @Override
    public EntityGrid getEntityGrid() {
        return entityGrid;
    }

    /**
     * Atribui o EntityActions.
     *
     * @param entityActions
     */
    @Override
    public void setEntityActions(EntityActions<?,? extends JPanel> entityActions) {
        EntityActions old = this.entityActions;
        this.entityActions = entityActions;
        changeSupport.firePropertyChange(
                PROP_ENTITY_ACTIONS, old, entityActions);
    }

    /**
     * Retorna o EntityActions.
     */
    @Override
    public EntityActions<?,? extends JPanel> getEntityActions() {
        return entityActions;
    }

    /**
     * Retorna a entidade selecionada no EntityGrid.
     * @return Retorna a entidade selecionada no EntityGrid.
     */
    @Override
    public Serializable getSelectedEntity() {
        
        EntityGrid eg = getEntityGrid();

        if (eg != null)
            return eg.getSelectedEntity();

        return null;
    }

    /**
     * Retorna o tipo de VisualEntity que o objeto é.
     */
    @Override
    public VisualEntityType getVisualEntityType() {
        return VisualEntityType.ENTITY_SELECTION;
    }

    /**
     * Retorna a representação visual.
     */
    @Override
    public JPanel getVisualRepresentation() {
        return visualRepresentation;
    }

    /**
     * Atribui a representação visual.
     * 
     * @param visualRepresentation
     */
    public void setVisualRepresentation(SelectionPanel visualRepresentation){
        SelectionPanel old = this.visualRepresentation;
        this.visualRepresentation = visualRepresentation;
        changeSupport.firePropertyChange(
                PROP_VISUAL_REPRESENTATION, old, visualRepresentation);
    }

}
